<?php

namespace app\admin\controller;

use think\Controller;
use  app\admin\model\Permission as PermissionModel;
use think\Db;
use think\Request;



class Permission extends Controller
{
    /**
     * 显示管理员列表页.
     *
     * @return \think\Response
     */
    public function index()
    {

        $user=new PermissionModel();
        $data=$user->getALL();
        return view('permission/index',[
            'title'=>'权限列表',
            'data'=>$data
        ]);

    }


    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //        return '添加表单';
        return view('permission/create', [
            'title'=>'填个权限!'
        ]);
    }



    /**
     * 保存新建的资源
     *
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $p=$request->post();
        $result = Db::name('permission')
            ->data($p)
            ->insert();
        if ($result>0) {
            return $this->success('添加成功', url('admin/permission/index'));
        } else {
            return $this->error('添加失败,请重试!!');
        }
    }



    /**
     * 删除权限
     * @param pid
     *
     * @return \think\Response
     */
    public function del($id)
    {


       $a= Db::table('hb_permission')->where('id',$id)->delete();

       $b= Db::table('role_permission')->where('p_id',$id)->delete();

       if ($a || $b){
           $this->echoResult(200,'succ',['id'=>$id]);
       }
    }

    private function echoResult($code="200",$msg="succ",$data=[]){
        $result = [
            "code"=>$code,
            "msg"=>$msg,
            "data"=>$data,
        ];
        echo json_encode($result);
        exit();
    }



    public function  edit($id)
    {
       $result= Db::table('hb_permission')->where('id',$id)->find();

       return $result;
    }

    public function  doedit(Request $request)
    {
        $p=$request->post();
//        dump($p);
      $result=  Db::table('hb_permission')->where('id', $p['id'])->update(['permission' => $p['permission'],'controller'=>$p['controller'],'method'=>$p['method']]);
      return  $p;
    }

}
